<?php

namespace App\Http\Controllers\Front;

use App\Http\Controllers\Controller;
use App\Model\UserWebPage;
use App\Model\UserWebPageContent;
use Illuminate\Http\Request;

class UserWebPageController extends Controller
{
    /**
     * 个人网页详情
     */
    public function detail(Request $request){

        $userPageDetail = UserWebPage::where('user_id', $request->user()->id)->select('id','logo', 'banner')->first();
        if ($userPageDetail) {
            $userPageDetail = $userPageDetail->toArray();
        }
        $userPageDetailContent = UserWebPageContent::where('user_id', $request->user()->id)->select('id', 'title', 'content', 'images')->orderBy('sort', 'asc')->get();
        if ($userPageDetailContent) {
            $userPageDetailContent = $userPageDetailContent->toArray();
        }

        return $this->renderJson(['logo_banner' => $userPageDetail ? $userPageDetail : '', 'content' => $userPageDetailContent ? $userPageDetailContent : '']);
    }

    /**
     * 个人网页详情1
     */
    public function getDetail(Request $request){
        $this->validate(
            $request,
            [
                'user_id' => 'required|int'
            ],
            [
                'required' => ':attribute不能为空'
            ],
            [
                'user_id' => 'user_id'
            ]
        );
        $userPageDetail = UserWebPage::where('user_id', $request->input('user_id'))->select('id','logo', 'banner')->first();
        if ($userPageDetail) {
            $userPageDetail = $userPageDetail->toArray();
        }
        $userPageDetailContent = UserWebPageContent::where('user_id', $request->input('user_id'))->select('id', 'title', 'content', 'images')->orderBy('sort', 'asc')->get();
        if ($userPageDetailContent) {
            $userPageDetailContent = $userPageDetailContent->toArray();
        }

        return $this->renderJson(['logo_banner' => $userPageDetail ? $userPageDetail : '', 'content' => $userPageDetailContent ? $userPageDetailContent : '']);
    }

    /**
     * 添加、修改保存logo
     */
    public function saveLogo(Request $request)
    {
        $this->validate(
            $request,
            [
                'logo' => 'required|string'
            ],
            [
                'required' => ':attribute不能为空'
            ],
            [
                'logo' => 'logo'
            ]
        );

        $webPageInfo = UserWebPage::where('user_id', $request->user()->id)->first();

        $result = false;
        if ($webPageInfo) {
            $webPageInfo->logo = $request->input('logo');
            $result = $webPageInfo->save();
        } else {
            $obj = new UserWebPage();
            $obj->user_id = $request->user()->id;
            $obj->logo = $request->input('logo');
            $result = $obj->save();
        }
        if ($result) {
            return $this->renderJson();
        } else {
            return $this->renderJson([], '-1', 'error');
        }
    }

    /**
     * 添加、修改保存banner
     */
    public function saveBanner(Request $request)
    {
        $this->validate(
            $request,
            [
                'banner' => 'required|string'
            ],
            [
                'required' => ':attribute不能为空'
            ],
            [
                'banner' => 'banner'
            ]
        );

        $webPageInfo = UserWebPage::where('user_id', $request->user()->id)->first();

        $result = false;
        if ($webPageInfo) {
            $webPageInfo->banner = $request->input('banner');
            $result = $webPageInfo->save();
        } else {
            $obj = new UserWebPage();
            $obj->user_id = $request->user()->id;
            $obj->banner = $request->input('banner');
            $result = $obj->save();
        }
        if ($result) {
            return $this->renderJson();
        } else {
            return $this->renderJson([], '-1', 'error');
        }
    }

    /**
     * 修改logo或者banner
     */
    public function updateLogoAndBanner(Request $request)
    {
        $banner = $request->input('banner');
        $logo = $request->input('logo');

        $useWebPage = new UserWebPage();
        $webPageInfo = $useWebPage->where('user_id', $request->user()->id)->first();

        if (!$webPageInfo) {
            if ($banner) {
                $useWebPage->banner = $banner;
            }
            if ($logo) {
                $useWebPage->logo = $logo;
            }
            $useWebPage->user_id = $request->user()->id;
            $result = $useWebPage->save();
            if ($result) {
                return $this->renderJson();
            } else {
                return $this->renderJson([], '-1', 'error');
            }
        }

        if ($banner) {
            $webPageInfo->banner = $banner;
        }
        if ($logo) {
            $webPageInfo->logo = $logo;
        }

        $result = $webPageInfo->save();
        if ($result) {
            return $this->renderJson();
        } else {
            return $this->renderJson([], '-1', 'error');
        }
    }

    /**
     * 添加图文或者视频
     */
    public function addContent(Request $request)
    {
        $this->validate(
            $request,
            [
                'title' => 'required|string'
            ],
            [
                'required' => ':attribute不能为空'
            ],
            [
                'title' => '标题'
            ]
        );

        $obj = new UserWebPageContent();
        $content = $obj->where(['user_id' => $request->user()->id])->orderBy('sort', 'desc')->select('sort')->first();

        if ($content) {
            $obj->sort = $content->sort + 1;
        }else{
            $obj->sort = 1;
        }

        $obj->user_id = $request->user()->id;
        $obj->title = $request->input('title');
        if ($request->input('content')) {
            $obj->content = $request->input('content');
        }
        if ($request->input('images')) {
            $obj->images = $request->input('images');
        }
        $obj->type = $request->input('type', '1');
        $result = $obj->save();
        if ($result) {
            return $this->renderJson();
        } else {
            return $this->renderJson([], '-1', 'error');
        }
    }

    /**
     * 编辑个人网页内容
     */
    public function editContent(Request $request)
    {
        $this->validate(
            $request,
            [
                'title' => 'required|string',
                'id' => 'required|int'
            ],
            [
                'required' => ':attribute不能为空',
            ],
            [
                'title' => '标题',
                'id' => 'id'
            ]
        );

        $contentObj = UserWebPageContent::where(['user_id' => $request->user()->id, 'id' => $request->input('id')])->first();

        if (!$contentObj) return $this->renderJson([], '-1', '不存在此数据');

        $contentObj->title = $request->input('title');
        $contentObj->content = $request->input('content');
        $contentObj->images = $request->input('images');
        $result = $contentObj->save();
        if ($result) {
            return $this->renderJson();
        } else {
            return $this->renderJson([], '-1', 'error');
        }
    }

    /**
     * 获取内容
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function getContent(Request $request)
    {
        $this->validate(
            $request,
            [
                'id' => 'required|int'
            ],
            [
                'required' => ':attribute不能为空',
            ],
            [
                'title' => '标题'
            ]
        );

        $contentObj = UserWebPageContent::where(['user_id' => $request->user()->id, 'id' => $request->input('id')])->select('id', 'title', 'content', 'images')->first();

        if (!$contentObj) return $this->renderJson([], '-1', '不存在此数据');

        return $this->renderJson($contentObj->toArray());
    }

    /**
     * 内容排序，上下移动
     */
    public function sort(Request $request)
    {
        $this->validate(
            $request,
            [
                'upper_id' => 'required',
                'down_id' => 'required',
            ],
            [
                'required' => ':attribute不能为空',
            ],
            [
                'upper_id' => '上移动',
                'down_id' => '下移动'
            ]
        );

        $upperObj = UserWebPageContent::where(['user_id' => $request->user()->id, 'id' => $request->input('upper_id')])->first();
        if (!$upperObj) return $this->renderJson([], '-1', '上移不存在此数据');

        if ($upperObj->sort == '1') { // 排序不能为1，已经是顶级了就不移动了
            return $this->renderJson([], '-1', '已经是顶级，不能在上移动了');
        }

        $downObj = UserWebPageContent::where(['user_id' => $request->user()->id, 'id' => $request->input('down_id')])->first();
        if (!$downObj) return $this->renderJson([], '-1', '下移不存在此数据');

        // 如果上移小于下移动就有问题了
        if ($upperObj->sort < $downObj->sort) {
            return $this->renderJson([], '-1', '上移数据不能小于下移动数据');
        }

        try {
            \DB::beginTransaction();

            $upperObj->decrement('sort'); // 上移动减sort
            $downObj->increment('sort'); // 下移动加sort

            \DB::commit();
            return $this->renderJson();
        } catch (\Exception $e) {
            \DB::rollBack();
            return $this->renderJson([], '-1', '数据保存失败'. $e->getMessage());
        }
    }

    // 置顶
    public function sortHeader(Request $request)
    {
        $this->validate(
            $request,
            [
                'id' => 'required|int',
            ],
            [
                'required' => ':attribute不能为空',
            ],
            [
                'id' => 'id',
            ]
        );
        $Obj = UserWebPageContent::where(['user_id' => $request->user()->id, 'id' => $request->input('id')])->first();
        if (!$Obj) return $this->renderJson([], '-1', '不存在此数据');
        if ($Obj->sort == '1') { // 排序不能为1，已经是顶级了就不移动了
            return $this->renderJson([], '-1', '已经是顶级，不能在上移动了');
        }
        try {
            \DB::beginTransaction();


            $Obj->sort = 1;
            $Obj->save();

            UserWebPageContent::where(['user_id' => $request->user()->id])->where('id', '<>', $Obj->id)->increment('sort');

            \DB::commit();
            return $this->renderJson();
        } catch (\Exception $e) {
            \DB::rollBack();
            return $this->renderJson([], '-1', '数据保存失败'. $e->getMessage());
        }
    }

    /**
     * 删除个人网页
     */
    public function deleteContent(Request $request)
    {
        $this->validate(
            $request,
            [
                'id' => 'required|int',
            ],
            [
                'required' => ':attribute不能为空',
            ],
            [
                'id' => 'id',
            ]
        );
        $Obj = UserWebPageContent::where(['user_id' => $request->user()->id, 'id' => $request->input('id')])->first();
        if (!$Obj) return $this->renderJson([], '-1', '不存在此数据');
        if ($Obj->delete()){
            return $this->renderJson();
        }else{
            return $this->renderJson([], '-1', '删除失败');
        }
    }
}